package com.example.common.room

import androidx.room.*
import kotlinx.coroutines.flow.Flow

/**
 * 视频点赞收藏数据访问对象
 */
@Dao
interface VideoFavoriteDao {
    
    /**
     * 获取所有收藏的视频
     */
    @Query("SELECT * FROM video_favorite WHERE isFavorited = 1 ORDER BY favoriteTime DESC")
    fun getAllFavoriteVideos(): Flow<List<VideoFavorite>>
    
    /**
     * 获取所有点赞的视频
     */
    @Query("SELECT * FROM video_favorite WHERE isLiked = 1 ORDER BY favoriteTime DESC")
    fun getAllLikedVideos(): Flow<List<VideoFavorite>>
    
    /**
     * 根据视频ID查询
     */
    @Query("SELECT * FROM video_favorite WHERE id = :videoId")
    fun getVideoById(videoId: Int): VideoFavorite?
    
    /**
     * 插入或更新视频
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertOrUpdate(video: VideoFavorite)
    
    /**
     * 更新点赞状态
     */
    @Query("UPDATE video_favorite SET isLiked = :isLiked WHERE id = :videoId")
    fun updateLikeStatus(videoId: Int, isLiked: Boolean): Int
    
    /**
     * 更新收藏状态
     */
    @Query("UPDATE video_favorite SET isFavorited = :isFavorited, favoriteTime = :favoriteTime WHERE id = :videoId")
    fun updateFavoriteStatus(videoId: Int, isFavorited: Boolean, favoriteTime: Long): Int
    
    /**
     * 删除视频
     */
    @Delete
    fun delete(video: VideoFavorite): Int
    
    /**
     * 根据ID删除视频
     */
    @Query("DELETE FROM video_favorite WHERE id = :videoId")
    fun deleteById(videoId: Int): Int
    
    /**
     * 删除所有视频
     */
    @Query("DELETE FROM video_favorite")
    fun deleteAll(): Int
    
    /**
     * 检查视频是否已点赞
     */
    @Query("SELECT isLiked FROM video_favorite WHERE id = :videoId")
    fun isVideoLiked(videoId: Int): Boolean?
    
    /**
     * 检查视频是否已收藏
     */
    @Query("SELECT isFavorited FROM video_favorite WHERE id = :videoId")
    fun isVideoFavorited(videoId: Int): Boolean?
}